// pages/page/return_good/After_sale_application/After_sale_application.js
var request_api = require('../../../../utils/require_api.js')
import Dialog from '../../../../youzan/youzan_dist/dialog/dialog';
import Toast from '../../../../youzan/youzan_dist/toast/toast';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    return_reason: [{
        name: '大小/颜色/型号等不合适'
      },
      {
        name: '发错货'
      }, {
        name: '质量问题'
      },
      {
        name: '商品与界面描述不符'
      }, {
        name: '商品损坏'
      },
      {
        name: '其他'
      },
    ],
    selectreason: '', //申请原因
    item: {}, //订单详情的某一款信息
    inputValue: '', //输入的内容
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.data.item = JSON.parse(options.item);

    this.setData({
      item: this.data.item,
      return_reason: this.data.return_reason
    })


  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  //隐藏申请原因
  hideModal(e) {
    this.data.selectreason = e.currentTarget.dataset.selectseason.name;
    this.setData({
      modalName: null,
      selectseason: e.currentTarget.dataset.selectseason.name
    })
  },

  //显示申请原因
  showModal(e) {
    this.setData({
      modalName: 'DrawerModalL'
    })
  },
  //输入问题描述触发
  reasonInput(e) {
    console.log(e);
    this.data.inputValue = e.detail.value;
  },
  //提交申请信息
  submitsome() {
    if (this.data.selectreason != '') {
      request_api.request(
        'insertreturnitem', //需要传给wxrequest.js 的 url
        {
          return_id: this.guid(),
          orderId: this.data.item.orderId,
          orderdetailid: this.data.item.id,
          userId: this.data.item.userId,
          status: '1',
          return_detail: this.data.inputValue,
          return_reason: this.data.selectreason,
          return_type: '未发货，申请退货',
          order_time: this.format_time(this.data.item.create_time),
        }, //需要传的 data
        function(res) {
          Toast.loading({
            mask: true,
            duration: 2000, // 持续展示 toast
            forbidClick: true, //禁用背景点击
            message: '提交中......',
            loadingType: 'spinner',
          });
          setTimeout(function() {
            Toast('您的审核已提交，等待卖家审核哦~~~');
          }, 2000);
          setTimeout(function() {
            wx.navigateBack({
              url: '../return_good',
            });
          }, 3000);
        },
        function() {
          console.log("錯誤");
        },
      )
    } else {


    }
  },


  format_time(time) {
    var date = new Date(time);
    let Y = date.getFullYear() + '-';
    let M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
    let D = date.getDate() + ' ';
    let h = date.getHours() + ':';
    let m = date.getMinutes() + ':';
    let s = date.getSeconds();
    return Y + M + D + h + m + s;
    console.log(Y + M + D + h + m + s); 
  },


  //生成唯一码
  guid() {
    return 'xxxxxxxxxxxxxxxxxxyxxxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
      var r = Math.random() * 16 | 0,
        v = c == 'x' ? r : (r & 0x3 | 0x8);
      return v.toString(16);
    });
  },


  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})